package com.javabasic.algorithm.leetcode;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/6/22 23:13
 */
public class ValidMountainArray {


    /**
     * 拟山数组,模拟题 O(n)
     * @param arr
     * @return
     */
    public boolean validMountainArray(int[] arr) {
        int len = arr.length;
        if (len < 3) {
            return false;
        }

        int pre = arr[0];
        int tagIndex = 0;
        for (int i = 1; i < len; i++) {
            if (arr[i] < pre) {
                tagIndex = i-1;
                break;
            } else if (arr[i] == pre) {
                /**
                 * 有相同元素直接返回false
                 */
                return false;
            }
            pre = arr[i];
        }
        if (tagIndex == 0) {
            return false;
        }
        pre = arr[tagIndex+1];
        for (int i = tagIndex + 2; i < len; i++) {
            if (arr[i] >= pre) {
                return false;
            }
            pre = arr[i];
        }

        return true;
    }
}
